package org.itsolutions.javaBackup.core;

import java.util.List;

import org.itsolutions.javaBackup.buffers.ByteBufferPool;
import org.itsolutions.javaBackup.buffers.ByteBuffers;
import org.itsolutions.javaBackup.buffers.SharedBufferProvider;
import org.itsolutions.javaBackup.core.tasks.FTPBackupTask;
import org.itsolutions.javaBackup.core.tasks.ITask;
import org.itsolutions.javaBackup.exceptions.AException;
import org.itsolutions.javaBackup.ioc.AJavaBackupModule;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;
import org.itsolutions.javaBackup.spi.EModule;
import org.itsolutions.javaBackup.spi.common.io.IByteBuffers;
import org.itsolutions.javaBackup.spi.common.io.ISharedBuffer;
import org.itsolutions.javaBackup.spi.core.INodeContextProvider;

/**
 * Core application module.
 * @author Tomas Fecko
 */
public final class CoreModule extends AJavaBackupModule {

	private static final ILogger LOGGER = LoggerHelper.getLogger(CoreModule.class);

	@Override
	protected void configure() {
		LOGGER.log(ELogLevel.DEBUG, "Configuring the CoreModule.");
		try {
			NodeContextProvider contextProvider = new NodeContextProvider();
			// we install all the needed modules upfront
			List<AJavaBackupModule> moduleInstances;
			moduleInstances = contextProvider.getModuleInstances();
			for (AJavaBackupModule module : moduleInstances) {
				LOGGER.log(ELogLevel.DEBUG, "Installing module:", module);
				install(module);
			}
			
			registerStartStopTask(CoreModuleStartStopTask.class);
			bind(CoreController.class).asEagerSingleton();
			bind(INodeContextProvider.class).toInstance(contextProvider);
			bind(ISharedBuffer.class).to(SharedBufferProvider.class).asEagerSingleton();
			bind(IByteBuffers.class).to(ByteBuffers.class);
			bind(ByteBufferPool.class).asEagerSingleton();

			bind(ITask.class).to(FTPBackupTask.class);
		} catch (AException e) {
			LOGGER.log(ELogLevel.ERROR, e);
		}
	}

	@Override
	protected EModule getModule() {
		return EModule.CORE;
	}
}





